#!/bin/bash
# 生成SSL证书脚本

echo "🔐 生成SSL证书用于HTTPS支持..."
echo "=================================="

# 检查mkcert是否安装
if ! command -v mkcert &> /dev/null; then
    echo "❌ mkcert未安装，请先安装mkcert"
    echo "安装方法："
    echo "  Ubuntu/Debian: sudo apt install mkcert"
    echo "  macOS: brew install mkcert"
    echo "  Windows: choco install mkcert"
    exit 1
fi

echo "✅ 发现mkcert，开始生成证书..."

# 安装CA证书（如果还没有安装）
echo "📋 安装CA证书..."
mkcert -install

# 生成SSL证书
echo "🔑 生成SSL证书..."
mkcert localhost 127.0.0.1 ::1

# 检查证书是否生成成功
if [ -f "localhost+2.pem" ] && [ -f "localhost+2-key.pem" ]; then
    echo "✅ SSL证书生成成功！"
    
    # 重命名证书文件
    mv localhost+2.pem localhost.pem
    mv localhost+2-key.pem localhost-key.pem
    
    echo "📁 证书文件："
    echo "  - localhost.pem (证书文件)"
    echo "  - localhost-key.pem (私钥文件)"
    
    echo ""
    echo "🌐 现在可以启动HTTPS服务："
    echo "  python3 app.py"
    echo ""
    echo "📱 移动端访问地址："
    echo "  https://localhost:5000"
    echo "  https://127.0.0.1:5000"
    echo ""
    echo "⚠️  注意："
    echo "  - 移动端需要访问您电脑的IP地址"
    echo "  - 例如：https://192.168.1.100:5000"
    echo "  - 确保防火墙允许5000端口"
    
else
    echo "❌ SSL证书生成失败"
    exit 1
fi
